package academic.emailClient.view;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import academic.emailClient.model.entities.Meeting;
import academic.emailClient.model.entities.MeetingAttributes;
import academic.emailClient.model.entities.MessageAttributes;

public class MergeMeeting extends JFrame {
	    private JRadioButton PurposeRbMine = new JRadioButton();
	    private JRadioButton purposeRbTheirs= new JRadioButton();
	    private JButton copyDescriptionBtn = new JButton();
	    private  JButton copyLocationBtn = new JButton();
	    private  JButton copyPurposeBtn = new JButton();;
	    private  JTextArea descriptionTextMine = new JTextArea();
	    private  JTextArea descriptionTextTheirs = new JTextArea();;
	    private  JRadioButton descrptionRbMine = new JRadioButton();
	    private  JRadioButton descrptionRbTheirs = new JRadioButton();
	    private  JButton resolveBtn = new JButton();
	    private  JButton cancelBtn= new JButton();
	    private  JPanel jPanel1 = new JPanel();
	    private  JPanel jPanel3 = new JPanel();
	    private  JScrollPane jScrollPane1 = new JScrollPane();
	    private  JScrollPane jScrollPane2 = new JScrollPane();
	    private  JScrollPane jScrollPane3= new JScrollPane();
	    private  JScrollPane jScrollPane4= new JScrollPane();
	    private  JScrollPane jScrollPane5= new JScrollPane();
	    private  JScrollPane jScrollPane6= new JScrollPane();
	    private  JRadioButton locationRbMine = new JRadioButton();
	    private  JRadioButton locationRbTheirs= new JRadioButton();
	    private  JTextArea locationTextMine = new JTextArea();
	    private  JTextArea locationTextTheirs = new JTextArea();
	    private  JTextArea purposeTextMine = new JTextArea();
	    private  JTextArea purposeTextTheirs = new JTextArea();
	    private Meeting mine;
	    private Meeting theirs;
    
	public MergeMeeting(Meeting mine, Meeting theirs){
		this.mine = mine;
		this.theirs = theirs;
	}
	
	public MergeMeeting(){
		
	}
	
	 public void BuildForm(Meeting mine, Meeting theirs, ArrayList<String> conflictedAreas)
	 {	    // Group the radio buttons.
	        ButtonGroup purposeGroup = new ButtonGroup();
	        purposeGroup.add(PurposeRbMine);
	        purposeGroup.add(purposeRbTheirs);
	        PurposeRbMine.setSelected(true);
	        
	        // Group the radio buttons.
	        ButtonGroup locationGroup = new ButtonGroup();
	        locationGroup.add(locationRbMine);
	        locationGroup.add(locationRbTheirs);
	        locationRbMine.setSelected(true);
	        
	        // Group the radio buttons.
	        ButtonGroup descriptionGroup = new ButtonGroup();
	        descriptionGroup.add(descrptionRbMine);
	        descriptionGroup.add(descrptionRbTheirs);
	        descrptionRbMine.setSelected(true);

	        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Theirs"));
	        jPanel1.setName(""); 
	        purposeRbTheirs.setText("Purpose");
	        descriptionTextTheirs.setEnabled(false);
	        locationRbTheirs.setText("Location");
	        locationTextTheirs.setEnabled(false);
	        descrptionRbTheirs.setText("Description");
	        purposeTextTheirs.setEnabled(false);
	        descriptionTextTheirs.setColumns(20);
	        descriptionTextTheirs.setRows(5);
	        jScrollPane1.setViewportView(descriptionTextTheirs);
	        locationTextTheirs.setColumns(20);
	        locationTextTheirs.setRows(5);
	        jScrollPane2.setViewportView(locationTextTheirs);
	        purposeTextTheirs.setColumns(20);
	        purposeTextTheirs.setRows(5);
	        jScrollPane3.setViewportView(purposeTextTheirs);
	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	        jPanel1.setLayout(jPanel1Layout);
	        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(locationRbTheirs)
            .addComponent(purposeRbTheirs)
            .addComponent(descrptionRbTheirs))
            .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jScrollPane2))
            .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
            .addContainerGap()))
	        );
	        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(purposeRbTheirs)
            .addGap(116, 116, 116)
            .addComponent(locationRbTheirs)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(30, 30, 30)
            .addComponent(descrptionRbTheirs)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
            .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(42, 42, 42)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(270, Short.MAX_VALUE)))
	        );

	        resolveBtn.setText("Resolve");
	        cancelBtn.setText("Cancel");
	        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Mine"));
	        jPanel3.setName(""); 
	        PurposeRbMine.setText("Purpose");
	        locationRbMine.setText("Location");
	        descrptionRbMine.setText("Description");
	        descriptionTextMine.setColumns(20);
	        descriptionTextMine.setRows(5);
	        jScrollPane4.setViewportView(descriptionTextMine);
	        locationTextMine.setColumns(20);
	        locationTextMine.setRows(5);
	        jScrollPane5.setViewportView(locationTextMine);
	        purposeTextMine.setColumns(20);
	        purposeTextMine.setRows(5);
	        jScrollPane6.setViewportView(purposeTextMine);
	        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
	        jPanel3.setLayout(jPanel3Layout);
	        jPanel3Layout.setHorizontalGroup(
	        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	       .addGroup(jPanel3Layout.createSequentialGroup()
	       .addContainerGap()
	       .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	       .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
	       .addGroup(jPanel3Layout.createSequentialGroup()
	       .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	       .addComponent(locationRbMine)
	       .addComponent(PurposeRbMine)
	       .addComponent(descrptionRbMine))
		   .addGap(0, 0, Short.MAX_VALUE))
	       .addComponent(jScrollPane5))
			.addContainerGap())
			.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(jPanel3Layout.createSequentialGroup()
			.addContainerGap()
			.addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
			.addContainerGap()))
	        );
	        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(PurposeRbMine)
            .addGap(116, 116, 116)
            .addComponent(locationRbMine)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(30, 30, 30)
            .addComponent(descrptionRbMine)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
            .addContainerGap())
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
            .addGap(42, 42, 42)
            .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(270, Short.MAX_VALUE)))
        );

	        copyPurposeBtn.setText("<");
	        copyLocationBtn.setText("<");
	        copyDescriptionBtn.setText("<");

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(222, 222, 222)
            .addComponent(resolveBtn)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(copyPurposeBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(copyLocationBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(copyDescriptionBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(cancelBtn)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(41, 41, 41))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(20, 20, 20)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(371, Short.MAX_VALUE)))
	        );
	        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
			.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
			.addGroup(layout.createSequentialGroup()
		    .addGap(82, 82, 82)
		    .addComponent(copyPurposeBtn)
		    .addGap(103, 103, 103)
		    .addComponent(copyLocationBtn)
		    .addGap(111, 111, 111)
            .addComponent(copyDescriptionBtn)))
            .addGap(26, 26, 26)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(resolveBtn)
            .addComponent(cancelBtn))
            .addContainerGap(26, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(75, Short.MAX_VALUE)))
	        );
	        
	        //Load Mine Content
	        LoadMineContent(mine);
	        //Load Theirs Content
	        LoadTheirsContent(theirs);
	        //Show conflicted areas
	        ShowConflictedAreas(conflictedAreas);
	        
	        setBounds(200, 200, 700, 600);
	        setVisible(true);
	        //setResizable(false);
	    }
	 
	 private void ShowConflictedAreas(ArrayList<String> conflictedAreas) {
		
        if(conflictedAreas.contains(MeetingAttributes.PURPOSE.name())){
			purposeTextMine.setBackground(Color.RED);
		}
		if(conflictedAreas.contains(MeetingAttributes.DESCRIPTION.name())){
			descriptionTextMine.setBackground(Color.RED);
		}
		if(conflictedAreas.contains(MeetingAttributes.LOCATION.name())){
			locationTextMine.setBackground(Color.RED);
		}	
		 
		boolean enablePurpose = false;
		boolean enableDescription = false;
		boolean enableLocation = false;
		
		for (String conflict : conflictedAreas){
			if (conflict.equals(MeetingAttributes.PURPOSE.name()))
				enablePurpose = true;
			if (conflict.equals(MeetingAttributes.LOCATION.name()))
				enableLocation = true;
			if (conflict.equals(MeetingAttributes.DESCRIPTION.name()))
				enableDescription = true;
		}

		purposeTextMine.setEnabled(enablePurpose);
		PurposeRbMine.setEnabled(enablePurpose);
		purposeRbTheirs.setEnabled(enablePurpose);
		copyPurposeBtn.setEnabled(enablePurpose);
	
		descriptionTextMine.setEnabled(enableDescription);
		descrptionRbMine.setEnabled(enableDescription);
		descrptionRbTheirs.setEnabled(enableDescription);
		copyDescriptionBtn.setEnabled(enableDescription);
		
		locationTextMine.setEnabled(enableLocation);
		locationRbMine.setEnabled(enableLocation);
		locationRbTheirs.setEnabled(enableLocation);
		copyLocationBtn.setEnabled(enableLocation);
		
	}

	private void LoadMineContent(Meeting mine) {
		 descriptionTextMine.setText(mine.getDescription());		
		 purposeTextMine.setText(mine.getPurpose());
		 locationTextMine.setText(mine.getLocation());
	}
	 
	 private void LoadTheirsContent(Meeting theirs) {
		 descriptionTextTheirs.setText(theirs.getDescription());		
		 purposeTextTheirs.setText(theirs.getPurpose());
		 locationTextTheirs.setText(theirs.getLocation());
	}


	public void setPurposeTextFieldMine(String text){
		 purposeTextMine.setText(text);
	 }
	 public void setPurposeTextFieldTheirs(String text){
		 purposeTextTheirs.setText(text);
	 }
	 public void setDescriptionTheirs(String text){
		 descriptionTextTheirs.setText(text);
	 }
	 public void setDescriptionMine(String text){
		 descriptionTextMine.setText(text);
	 }
	 public void setLocationMine(String text){
		 locationTextMine.setText(text);
	 }
	 public void setLocationTheirs(String text){
		 locationTextTheirs.setText(text);
	 }
	 
	 public String getDescriptionMine(){
		 return descriptionTextMine.getText();
	 }
	 public String getLocationMine(){
		 return locationTextMine.getText();
	 }
	 public String getPurposeMine(){
		 return purposeTextMine.getText();
	 }

	public void copyLocation() {
		locationTextMine.append("\n");
		locationTextMine.append(locationTextTheirs.getText());		
	}
	
	public void copyPurpose() {
		purposeTextMine.append("\n");
		purposeTextMine.append(purposeTextTheirs.getText());		
	}
	
	public void copyDescription() {
		descriptionTextMine.append("\n");
		descriptionTextMine.append(descriptionTextTheirs.getText());
	}

	public JButton getCopyPurposeButton() {
		return copyPurposeBtn;		
	}
	
	public JButton getCopyDescriptionButton() {
		return copyDescriptionBtn;		
	}
	
	public JButton getCopyLocationButton() {
		return copyLocationBtn;		
	}
	
	public JButton getResolveButton() {
		return resolveBtn;		
	}
	
	public JButton getCancelButton() {
		return cancelBtn;		
	}
	
	public void closeForm(){
		this.closeForm();
	}
}



